package algorithm.offer;

import org.junit.Test;

public class offer28 {

    @Test
    public void test01() {
        System.out.println("work");

        String s = "aaabaaab";
        int[] next = new int[s.length()];
        getNext(next, s);
        for (int i : next) {
            System.out.println(i);
        }

    }

    public void getNext(int[] next, String s) {
        int j = -1;
        next[0] = j;
        for (int i = 1; i < s.length(); i++) {
            while (j >= 0 && s.charAt(i) != s.charAt(j + 1)) {
                j = next[j];
            }

            if (s.charAt(i) == s.charAt(j + 1)) {
                j++;
            }
            next[i] = j;
        }
    }

}
